package com.whl.sm.service.impl.assets;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.common.result.Result;
import com.whl.sm.mapper.assets.ComUnitsMapper;
import com.whl.sm.model.assets.ComBuilding;
import com.whl.sm.model.assets.ComUnits;
import com.whl.sm.model.inhabitant.AssetsOwner;
import com.whl.sm.model.inhabitant.AssetsTenant;
import com.whl.sm.model.system.OrgUserInfo;
import com.whl.sm.service.assets.ComBuildingService;
import com.whl.sm.service.assets.ComUnitsService;
import com.whl.sm.service.inhabitant.AssetsOwnerService;
import com.whl.sm.service.inhabitant.AssetsTenantService;
import com.whl.sm.service.system.OrgUserInfoService;
import com.whl.sm.service.system.OrgUserService;
import com.whl.sm.vo.OrgUserInfoVo;
import com.whl.sm.vo.WxAssetsDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-09
 */
@Service
public class ComUnitsServiceImpl extends ServiceImpl<ComUnitsMapper, ComUnits> implements ComUnitsService {

    @Autowired
    private OrgUserService orgUserService;

    @Autowired
    private OrgUserInfoService orgUserInfoService;

    @Autowired
    private ComBuildingService comBuildingService;

    @Autowired
    private AssetsOwnerService assetsOwnerService;

    @Autowired
    private AssetsTenantService assetsTenantService;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public int update(ComUnits comUnits) {
        int result;
        comUnits.setOperator(orgUserService.getCurrentUserId());
        if (StringUtils.isNotBlank(comUnits.getId())){
            getBaseMapper().updateById(comUnits);
            result=1;
        }
        else {
            getBaseMapper().insert(comUnits);
            result=2;
        }
        return result;
    }

    @Override
    public ComUnits getByUnitCode(String unitCode) {
        ComUnits data = null;
        List<ComUnits> unitList = getBaseMapper().selectList(new QueryWrapper<ComUnits>().eq("unit_code",unitCode));
        if (unitList.size()>0){
            data  = unitList.get(0);
        }
        return data;
    }

    @Override
    public Result getDetailByCurrent() {
        String userId = orgUserService.getCurrentUserId();
        String unitId = null;
        List<AssetsOwner> owners = assetsOwnerService.list(new QueryWrapper<AssetsOwner>().eq("user_id",userId));
        if (owners.size() > 0){
            unitId = owners.get(0).getAssetsId();
        }
        else {
            List<AssetsTenant> tenants = assetsTenantService.list(new QueryWrapper<AssetsTenant>().eq("user_id",userId));
            if (tenants.size() == 0){
                return Result.fail();
            }
            unitId = tenants.get(0).getAssetsId();
        }
        ComUnits linkUnit = getBaseMapper().selectById(unitId);
        ComBuilding linkBuilding = comBuildingService.getById(linkUnit.getBudId());
        List<OrgUserInfo> memberList = new ArrayList<>();
        WxAssetsDetailVo rtValue = new WxAssetsDetailVo(linkUnit,linkBuilding);
        if (linkUnit.getUnitStatus() == 1){
            List<AssetsOwner> members = assetsOwnerService.list(new QueryWrapper<AssetsOwner>()
                    .eq("assets_id",unitId)
                    .orderByDesc("owner_type")
            );
            memberList = members.stream().map(obj->{
                return orgUserInfoService.getByUserId(obj.getUserId());
            }).collect(Collectors.toList());

        }
        else if(linkUnit.getUnitStatus() == 2){
            List<AssetsTenant> members = assetsTenantService.list(new QueryWrapper<AssetsTenant>().eq("assets_id",unitId));
            rtValue.setValidTime(members.get(0).getValidTime());
            memberList = members.stream().map(obj->{
                return orgUserInfoService.getByUserId(obj.getUserId());
            }).collect(Collectors.toList());
        }
        rtValue.setMemberList(memberList);
        return Result.ok(rtValue);
    }
}
